package org.linlinjava.litemall.db.dao;

import org.apache.ibatis.annotations.*;
import org.linlinjava.litemall.db.domain.Device;

import java.util.List;

@Mapper
public interface DeviceMapper {

    // 查询设备（支持动态条件）
    @Select("<script>" +
            "SELECT * FROM devices " +
            "<where>" +
            "<if test='ctrlDeviceIdentification != null'> AND ctrl_device_identification = #{ctrlDeviceIdentification}</if>" +
            "<if test='connectStatus != null'> AND connect_status = #{connectStatus}</if>" +
            "</where>" +
            "</script>")
    List<Device> selectByConditions(@Param("ctrlDeviceIdentification") String ctrlDeviceIdentification,
                                    @Param("connectStatus") String connectStatus);

    // 插入设备（自动处理主键）
    @Insert("INSERT INTO devices (device_identification, device_name, device_description, " +
            "connect_status, remark, ctrl_device_identification, cuo_device_rel_status, " +
            "create_time, update_time, latitude, longitude) " +
            "VALUES (#{deviceIdentification}, #{deviceName}, #{deviceDescription}, " +
            "#{connectStatus}, #{remark}, #{ctrlDeviceIdentification}, " +
            "#{cuoDeviceRelStatus}, #{createTime}, #{updateTime}, #{latitude}, #{longitude})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Device device);

    // 更新设备信息
    @Update("<script>" +
            "UPDATE devices " +
            "<set>" +
            "<if test='deviceIdentification != null'>device_identification = #{deviceIdentification},</if>" +
            "<if test='deviceName != null'>device_name = #{deviceName},</if>" +
            "<if test='deviceDescription != null'>device_description = #{deviceDescription},</if>" +
            "<if test='connectStatus != null'>connect_status = #{connectStatus},</if>" +
            "<if test='remark != null'>remark = #{remark},</if>" +
            "<if test='ctrlDeviceIdentification != null'>ctrl_device_identification = #{ctrlDeviceIdentification},</if>" +
            "<if test='cuoDeviceRelStatus != null'>cuo_device_rel_status = #{cuoDeviceRelStatus},</if>" +
            "<if test='createTime != null'>create_time = #{createTime},</if>" +
            "<if test='updateTime != null'>update_time = #{updateTime},</if>" +
            "<if test='latitude != null'>latitude = #{latitude},</if>" +
            "<if test='longitude != null'>longitude = #{longitude}</if>" +
            "</set>" +
            "<where>id = #{id}</where>" +
            "</script>")
    int update(Device device);

    // 删除设备
    @Delete("DELETE FROM devices WHERE id = #{id}")
    int deleteById(Long id);

    // 根据ID查询设备
    @Select("SELECT * FROM devices WHERE id = #{id}")
    Device selectById(Long id);

    // 查询所有设备
    @Select("SELECT * FROM devices")
    List<Device> selectAll();
}